<?php
namespace app\admin\controller;
use cmf\controller\AdminBaseController;
use think\Db;

class FbfansController extends AdminBaseController
{
	public function index()
	{
		$content = hook_one('admin_fbfans_index_view');
		if (!empty($content)) {
			return $content;
		}

		$where = ['id' => ['neq', 0]];
		/**搜索条件**/
		$uname = $this->request->param('uname');
		if ($uname) {
			$where['uname'] = ['like', "%$uname%"];
		}

		$fbfansList = Db::name('fbfans')
			->where($where)
			->order("id DESC")
			->paginate(10);

		$fbfansList->appends(['uname' => $uname]);
		// 获取分页显示
		$page = $fbfansList->render();

		$this->assign("page", $page);
		$this->assign("fbfansList", $fbfansList);
		return $this->fetch();
	}

	public function add()
	{
		return $this->fetch();
	}

	public function addPost()
	{
		if ($this->request->isPost()) {
			if (!empty($_POST['uname']) || !empty($_POST['fburl'])) {
				$_POST["create_time"] = date("Y-m-d H:i:s");
				$_POST["update_time"] = date("Y-m-d H:i:s");
				$_POST['fburl'] = str_replace("，", ",", $_POST['fburl']);
				$domainName = explode(',', $_POST['fburl']);
				foreach ($domainName as $k => $v) {
					if(!$v) continue;
					$_POST['fburl'] = trim($v);
					$result	= DB::name('fbfans')->insertGetId($_POST);
				}
				if ($result !== false) {
						$this->success("添加成功！", url("fbfans/index"));
					} else {
						$this->error("添加失败！");
					}
			} else {
				$this->error("站长姓名能为空！");
			}
		}
	}

	public function edit()
	{
		$content = hook_one('admin_fbfans_edit_view');

		if (!empty($content)) {
			return $content;
		}

		$id      = $this->request->param('id', 0, 'intval');
		$fbfans = DB::name('fbfans')->where(["id" => $id])->find();
		$this->assign($fbfans);
		return $this->fetch();
	}

	public function editPost()
	{
		if ($this->request->isPost()) {
			if (!empty($_POST['id'])) {
				$_POST["update_time"] = date("Y-m-d H:i:s");
				

				$result = DB::name('fbfans')->update($_POST);
				if ($result !== false) {
						$this->success("添加成功！", url("fbfans/index"));
					} else {
						$this->error("添加失败！");
					}
			} else {
				$this->error("ID不能为空！");
			}
		}
	}

	public function delete()
	{
		$id = $this->request->param('id', 0, 'intval');

		if (Db::name('fbfans')->delete($id) !== false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}
}
